<template>
  <div class="todo-footer">
    <label>
      <input
        type="checkbox"
        :checked="doneTotal === allTotal"
        @change="updateAllTodos"
      />
    </label>
    <span>
      <span>已完成{{ doneTotal }}</span> / 全部{{ allTotal }}
    </span>
    <button class="btn btn-danger" @click="clearAllDone">清除已完成任务</button>
  </div>
</template>

<script>
import PubSub from 'pubsub-js'
export default {
  props: ['list'],
  computed: {
    // 数据总数
    allTotal() {
      return this.list.length
    },
    // 已完成数据总数
    doneTotal() {
      return this.list.filter((item) => item.isDone).length
    },
  },
  methods: {
    // 全选按钮的事件处理函数
    updateAllTodos(e) {
      //通知app组件,将todos中每一个对面的isDone改成目标值
      PubSub.publish('checkAll', e.target.checked)
    },
    // 清除已完成任务的事件处理函数
    clearAllDone() {
      PubSub.publish('clearAllDone')
    },
  },
}
</script>

<style>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
